/*
 * Consumer.h
 *
 *  Created on: Feb 6, 2010
 *      Author: Jamie LaPointe
 */

#ifndef CONSUMER_H_
#define CONSUMER_H_

#include "ProducerConsumerShared.h"

namespace ProducerConsumer
{

namespace Consumer
{

class Consumer
{
private:
   Common::ProducerConsumerShared * producerConsumerShared_;
public:
   Consumer(Common::ProducerConsumerShared * const producerConsumerShared)
   {
      producerConsumerShared_ = producerConsumerShared;
   }

   Common::Request run()
   {
      pthread_mutex_lock(&producerConsumerShared_->mutex);

      while (producerConsumerShared_->requestList.size() < 1)
      {
         pthread_cond_wait(&producerConsumerShared_->consumerCond,
               &producerConsumerShared_->mutex);
      }

      Common::Request request = producerConsumerShared_->requestList.back();
      producerConsumerShared_->requestList.pop_back();

      pthread_mutex_unlock(&producerConsumerShared_->mutex);
      pthread_cond_signal(&producerConsumerShared_->producerCond);

      return request;
   }
}; // class Consumer

} // namespace Consumer

} // namespace ProducerConsumer

#endif /* CONSUMER_H_ */
